<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 商品订单模型
 *
 * @author Miluo Huang
 *
 * @SWG\Model(id="Order")
 * @SWG\Property(name="id", type="string",description="订单号")
 */
class Order extends Model
{
    use SoftDeletes;

    protected $table = 'orders';

    static $status = [
        'ToBePrinted' => '待打印',
        'Normal'      => '正常',
        'Deny'        => '拒签',
        'Completed'   => '完成',
        'Canceled'    => '取消'
    ];

    static $step = [
        'WaitDeliverGoods' => '等待发货',
        'DeliverGoods'     => '已发货',
        'Exchange'         => '换货',
        'Return'           => '退货',
        'Completed'        => '完成'
    ];

    static $pay_mode = [
        1 => '货到付款'
    ];


    public $incrementing = false;

    public function __construct()
    {
        parent::__construct();

        // 自动生成订单ID。
        $this->attributes['id'] = uniqueid();
    }

    public static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            // 创建订单时给予默认订单状态与默认订单步骤。
            if (empty($model->status)) {
                $model->status = 'Normal';
            }
            if (empty($model->step)) {
                $model->step = 'WaitDeliverGoods';
            }

            // 查询是否下过订单
            $order = Order::where('member_id', $model->member_id)->first();
            if (!is_null($order)) {
                $model->repeat_tag = 1;
            }
        });

        static::created(function ($model) {
        });

        static::deleted(function () {
            // 禁止删除。
            return false;
        });

        static::updating(function ($model) {
        });
    }

    /**
     * 所属会员
     */
    public function member()
    {
        return $this->belongsTo('App\Models\Member');
    }

    /**
     * 地区
     */
    public function getLocationAttribute()
    {
        $location = '';
        $province = Location::find($this->province_id);
        $city     = Location::find($this->city_id);
        $area     = Location::find($this->area_id);
        !is_null($province) ? $location .= $province->name : '';
        !is_null($city) ? $location .= ' ' . $city->name : '';
        !is_null($area) ? $location .= ' ' . $area->name : '';

        return $location;
    }

    /**
     * 渠道
     */
    public function channel()
    {
        return $this->belongsTo('App\Models\ChannelSource', 'channel_id');
    }

    /**
     * 信息
     */
    public function getRedefinitionInfoAttribute()
    {
        $info = json_decode($this->info, true);
        if (is_null($info)) {
            return $this->info;
        }
        $redefinition_info = '';
        foreach ($info as $key => $item) {
            $redefinition_info .= $key . '：' . $item . '<br>';
        }

        return $redefinition_info;
    }

    /**
     * 信息
     */
    public function getOrderGoodsInfoAttribute()
    {
        $info = json_decode($this->info, true);
        if (is_null($info)) {
            return $this->info;
        }
        $redefinition_info = '';
        foreach ($info as $key => $item) {
            if ($key != '货品名') {
                $redefinition_info .= $key . '：' . $item . '<br>';
            }
        }

        return $redefinition_info;
    }

    /**
     * 商品
     */
    public function goods()
    {
        return $this->belongsToMany('App\Models\Goods', 'order_goods', 'order_id', 'goods_id')->withTimestamps();
    }


    /**
     * 订单回访
     *
     * @return string
     */
    public function getFollowsAttribute()
    {
        $follows     = OrderFollow::where('order_id', $this->id)->get();
        $f_count     = count($follows);
        $follows_str = '';
        foreach ($follows as $key => $item) {
            $follows_str .= $item->content;
            if ($key < ($f_count - 1)) {
                $follows_str .= '<br>';
            }
        }

        return $follows_str;
    }

    /**
     * 手机号归属地
     */
    public function getPhoneBelongAttribute()
    {
        $mobile = $this->member->mobile;
        $mobile = substr($mobile,0,7);
        $phone_numeber = \DB::connection('apollo_mysql')->select("select * from phonenumber_info where phone_header= '".$mobile."'");
        $r = 0;
        if(empty($phone_numeber)){
            return $r;
        }
        $info = $phone_numeber[0];

        if(strspn($this->address,$info->city) !== false || strspn($this->Location,$info->city) !== false){
            $r = 1;
        }

        return $r;
    }
}